package com.hxit.huaxiamelongarden.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @ClassName OnlineStatusManager
 * @description 在线状态管理（使用Redis）
 * @Author 12694
 * @Date 2025/1/30 20:22
 * @Version 1.0
 **/
@Component
public class OnlineStatusManager {
    private static final String ONLINE_KEY = "online:users";

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public void userLogin(Long userId) {
        redisTemplate.opsForSet().add(ONLINE_KEY, userId.toString());
    }

    public void userLogout(Long userId) {
        redisTemplate.opsForSet().remove(ONLINE_KEY, userId.toString());
    }

    public boolean isUserOnline(Long userId) {
        return Boolean.TRUE.equals(
                redisTemplate.opsForSet().isMember(ONLINE_KEY, userId.toString())
        );
    }
}
